package com.co.notaweb.beans.system;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.co.notaweb.constantes.ConstantesMsg;
import com.co.notaweb.entities.Persona;
import com.co.notaweb.facade.system.ISistemaFacadeLocal;
import com.co.notaweb.util.Constants;
import com.co.notaweb.util.FacesUtils;
import com.co.notaweb.util.ServiceLocator;

public class LoginBean {

	private ISistemaFacadeLocal sistemaFacade;

	private Principal usuario;
	private Persona persona;

	@PostConstruct
	private void init() {
		try {
			sistemaFacade = (ISistemaFacadeLocal) new ServiceLocator()
					.getServiceLocal(Constants.FACADESISTEMA);
			persona = getPersona();
			if (persona != null) {
				usuario = new Principal();
				usuario.setNombre(persona.getNombreCompleto());
				usuario.setUsername(persona.getUsuario().getUsername());
				usuario.setId(persona.getId());
				usuario.setIdUsuario(persona.getUsuario().getId());
			}

		} catch (Exception e) {
			FacesUtils.addErrorMessageBundle(ConstantesMsg.COMMON_ERROR);
			e.printStackTrace();
		}
	}

	public boolean isRolAdmin() {
		return getRequest().isUserInRole(Constants.ROL_ADMINISTRADOR);
	}

	public boolean isRolEstudiante() {
		return getRequest().isUserInRole(Constants.ROL_ESTUDIANTE);
	}

	public boolean isRolDocente() {
		return getRequest().isUserInRole(Constants.ROL_DOCENTE);
	}
	
	public boolean isRolUsuario() {
		return getRequest().isUserInRole(Constants.ROL_USUARIO);
	}
	

	public boolean isLoggedIn() {
		init();
		return persona == null ? false : true;
	}

	public String logOut() throws ServletException, IOException {
		FacesContext context = FacesContext.getCurrentInstance();
	    ExternalContext externalContext = context.getExternalContext();

	    // invalidate session
	    Object session = externalContext.getSession(false);
	    HttpSession httpSession = (HttpSession) session;
	    httpSession.invalidate();
		
		return "logout";
	}

	private HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}

	/**
	 ******************************************************************************************************************** 
	 * Getters And Setters
	 ******************************************************************************************************************** 
	 */

	public Principal getUsuario() {
		init();
		return usuario;
	}

	public void setUsuario(Principal usuario) {
		this.usuario = usuario;
	}

	public Persona getPersona() {
		if (FacesContext.getCurrentInstance() != null) {
			ExternalContext context = FacesContext.getCurrentInstance()
					.getExternalContext();
			if (context.getUserPrincipal() != null) {
				String usuario = context.getUserPrincipal().getName();
				persona = sistemaFacade.findPersona(usuario);
			}
		}

		return persona;
	}

	public void setPersona(Persona persona) {
		this.persona = persona;
	}

}
